//
//  UILabel+PPLabel.m
//  pickpic
//
//  Created by 胡文广 on 2017/6/10.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "UILabel+PPLabel.h"

@implementation UILabel (PPLabel)

- (void)changeTitleMutableParagraphStyle:(NSMutableParagraphStyle *)paragraphStyle titleText:(NSString *)titleText labelFont:(UIFont *)font{
    
    if (titleText.length<=0) {
        return;
    }
    
    NSDictionary *dic = @{NSFontAttributeName:font,
                          NSParagraphStyleAttributeName:paragraphStyle
                          };
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:titleText attributes:dic];
//     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleText];
    [attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [titleText length])];
    
    self.attributedText = attributeStr;
    [self sizeToFit];
    
}

- (void)changeLineSpaceForLabelWithSpace:(float)space text:(NSString *)text labelFont:(UIFont *)font{
    
    if (text.length<=0) {
        return;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    NSDictionary *dic = @{NSFontAttributeName:font,
                          NSParagraphStyleAttributeName:paragraphStyle
                          };
     NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text attributes:dic];
    self.attributedText = attributeStr;
    [self sizeToFit];
    
}

- (void)changeLineSpaceForLabelText:(NSString *)labelText WithSpace:(float)space {
    
    if (labelText.length<=0) {
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    [self sizeToFit];
    
}

+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

#pragma mark -计算对齐方式
- (void)textLabelAlbumtitle:(NSString *)albumtitle width:(CGFloat)width{
    
    //大于width左对齐 否则居中
    
    CGFloat orgWidth = [self rectWidthWithText:albumtitle height:21 font:self.font];
    
    if (orgWidth > width) {
        self.textAlignment = NSTextAlignmentLeft;
    }else{
        self.textAlignment = NSTextAlignmentCenter;
    }
    
    
}

- (BOOL)labelText:(NSString *)text width:(CGFloat)width{

    CGFloat orgWidth = [self rectWidthWithText:text height:21 font:self.font];
    
    if (orgWidth > width) {
        return YES;
    }
    return NO;
}
@end
